<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\MemberVipOrder;
use App\Admin\RowActions\VipOrder\Refund;
use App\Constants\VipOrderStatusConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberVipOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberVipOrder(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->column('id')->sortable();
            $grid->column('member_id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('order_sn');
            $grid->column('pay_sn');
            $grid->column('pay_amount')->display(function ($amount){
                return bcmul($amount, 0.01, 2);
            });
            $grid->column('pay_time')->display(function ($time){
                return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
            });
            $grid->column('status')->using(VipOrderStatusConstant::MAP)->dot(['danger', 'success', 'warning', 'info']);
            $grid->column('vip', 'vip信息')->view('admin.vip_order.vip_info');
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                if ($actions->row->status == VipOrderStatusConstant::IS_PAY) {
                    $actions->append(new Refund(\App\Models\MemberVipOrder::class));
                }
            });

            $grid->filter(function (Grid\Filter $filter) use($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('member_id')->width(2);
                $filter->like('order_sn')->width(2);
                $filter->like('pay_sn')->width(2);
                $filter->equal('status')->select(VipOrderStatusConstant::MAP)->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberVipOrder(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('mini_type');
            $show->field('order_sn');
            $show->field('pay_sn');
            $show->field('pay_amount');
            $show->field('pay_time');
            $show->field('status');
            $show->field('vip_id');
            $show->field('vip_price');
            $show->field('vip_day');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberVipOrder(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('mini_type');
            $form->text('order_sn');
            $form->text('pay_sn');
            $form->text('pay_amount');
            $form->text('pay_time');
            $form->text('status');
            $form->text('vip_id');
            $form->text('vip_price');
            $form->text('vip_day');
            $form->text('start_time');
            $form->text('end_time');
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
